(function ($) {
    window.admin = {};
    admin.utils = {};
    admin.article = {};
    admin.category = {};
    admin.comment = {};
    admin.tag = {};
    admin.link = {};

    admin.UEditor = function (id) {
        UE.getEditor(id, {
            toolbars: [[
                'fullscreen', 'source', '|', 'undo', 'redo', '|',
                'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
                'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
                'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
                'directionalityltr', 'directionalityrtl', 'indent', '|',
                'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
                'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
                'emotion', 'scrawl', 'music', 'map', 'gmap', 'insertframe', 'insertcode', 'webapp', 'pagebreak', 'template', 'background', '|',
                'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|',
                'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|',
                'print', 'preview', 'searchreplace', 'drafts'
            ]]
        });
    };

    admin.editor = function (id) {
        admin.article.editor = KindEditor.create('#' + id, {
            cssPath: [
                window.variables.base + '/script/kindeditor/plugins/code/prettify.css'
            ]
        });
    };

    ////////////////////////////////////////////////////////////
    //admin.utils
    ////////////////////////////////////////////////////////////
    admin.utils.showLoading = function(){
        $('<div class="datagrid-mask" style="z-index:1000"></div>').css({
            display: 'block',
            width: '100%',
            height: $(window).height()
        }).appendTo($(window.top.document.body));
        $('<div class="datagrid-mask-msg"></div>').html('加载中，请稍后...').appendTo($(window.top.document.body)).css({
            display: 'block',
            left: ($(document.body).outerWidth(true) - 190) / 2,
            top: ($(window).height() - 45) / 2
        });
    };

    admin.utils.hideLoading = function(){
        $('.datagrid-mask', window.top.document).remove();
        $('.datagrid-mask-msg', window.top.document).remove();
    };

    ////////////////////////////////////////////////////////////
    //admin.article
    ////////////////////////////////////////////////////////////

    /**
     * 删除文章
     * @param id
     */
    admin.article.delete = function (id) {
        if (confirm("确认删除该条数据吗？")) {
            httpPost('/admin/article/delete/do', {'id': id}, function (data) {
                if (data == true) {
                    $('#articleForm').submit();
                }
            });
        }
    };


    /**
     * 文章分类表单验证
     */
    admin.article.validateForm = function () {
        $('#articleForm').validate({
            wrapper: 'li',
            errorLabelContainer: '#error',
            rules: {
                title: {
                    required: true,
                    maxlength: 50
                },
                category: {
                    required: true
                },
                content: {
                    required: true
                }
            },
            messages: {
                name: {
                    required: '请填写文章标题',
                    maxlength: '分类名称最多不能超过{0}个字'
                },
                category: {
                    required: '请选择文章分类'
                },
                content: {
                    required: '文章内容不能为空'
                }
            }
        });
    }


    ////////////////////////////////////////////////////////////
    //admin.category
    ////////////////////////////////////////////////////////////

    /**
     * 删除分类
     *
     * @param id
     */
    admin.category.delete = function (id) {
        if (confirm("确认删除该条数据吗？")) {
            httpPost('/admin/category/delete/do', {'id': id}, function (data) {
                if (data == true) {
                    $('#categoryForm').submit();
                }
            });
        }
    };


    /**
     * 文章分类表单验证
     */
    admin.category.validateForm = function () {
        $('#categoryForm').validate({
            wrapper: 'li',
            errorLabelContainer: '#error',
            rules: {
                name: {
                    required: true,
                    maxlength: 12
                },
                description: {
                    maxlength: 500
                }
            },
            messages: {
                name: {
                    required: '请填写分类名称',
                    maxlength: '分类名称最多不能超过{0}个字'
                },
                description: {
                    maxlength: '描述最多不能超过{0}个字'
                }
            }
        });
    }


    ////////////////////////////////////////////////////////////
    //admin.comment
    ////////////////////////////////////////////////////////////

    /**
     * 删除评论
     *
     * @param id
     */
    admin.comment.delete = function (id) {
        if (confirm("确认删除该条数据吗？")) {
            httpPost('/admin/comment/delete/do', {'id': id}, function (data) {
                if (data == true) {
                    $('#commentForm').submit();
                }
            });
        }
    };


    /**
     * 标记为垃圾
     *
     * @param id
     */
    admin.comment.spam = function (id) {
        if (confirm("确认讲该数据标记为垃圾数据吗？")) {
            httpPost('/admin/comment/spam/do', {'id': id}, function (data) {
                if (data == true) {
                    $('#commentForm').submit();
                }
            });
        }
    };

    /**
     * 标记为垃圾
     *
     * @param id
     */
    admin.comment.recover = function (id) {
        httpPost('/admin/comment/recover/do', {'id': id}, function (data) {
            if (data == true) {
                $('#commentForm').submit();
            }
        });
    };


    ////////////////////////////////////////////////////////////
    //admin.tag
    ////////////////////////////////////////////////////////////

    /**
     * 删除标签
     *
     * @param id
     */
    admin.tag.delete = function (id) {
        if (confirm("确认删除该条数据吗？")) {
            httpPost('/admin/tag/delete/do', {'id': id}, function (data) {
                if (data == true) {
                    $('#tagForm').submit();
                }
            });
        }
    };


    /**
     * 标签表单验证
     */
    admin.tag.validateForm = function () {
        $('#tagForm').validate({
            wrapper: 'li',
            errorLabelContainer: '#error',
            rules: {
                name: {
                    required: true,
                    maxlength: 12
                },
                description: {
                    maxlength: 500
                }
            },
            messages: {
                name: {
                    required: '请填写标签名称',
                    maxlength: '标签名称最多不能超过{0}个字'
                },
                description: {
                    maxlength: '描述最多不能超过{0}个字'
                }
            }
        });
    }


    ////////////////////////////////////////////////////////////
    //admin.link
    ////////////////////////////////////////////////////////////

    /**
     * 删除链接
     *
     * @param id
     */
    admin.link.delete = function (id) {
        if (confirm("确认删除该条数据吗？")) {
            httpPost('/admin/link/delete/do', {'id': id}, function (data) {
                if (data == true) {
                    $('#linkForm').submit();
                }
            });
        }
    };


    /**
     * 链接表单验证
     */
    admin.link.validateForm = function () {
        $('#linkForm').validate({
            wrapper: 'li',
            errorLabelContainer: '#error',
            rules: {
                name: {
                    required: true,
                    maxlength: 32
                },
                url: {
                    required: true,
                    maxlength: 1024,
                    url: true
                },
                description: {
                    maxlength: 500
                }
            },
            messages: {
                name: {
                    required: '请填写链接名称',
                    maxlength: '链接名称最多不能超过{0}个字'
                },
                url: {
                    required: '请填写链接地址',
                    maxlength: '链接地址最多不能超过{0}个字',
                    url: "链接地址必须是一个合法的URL"
                },
                description: {
                    maxlength: '描述最多不能超过{0}个字'
                }
            }
        });
    }
})(jQuery);
